
import Pagination from "@/app/components/Pagination";
import IssueAction from "@/app/issues/IssueAction";
import IssueTable, {columnNames, IssueQuery} from "@/app/issues/IssueTable";
import prisma from "@/prisma/client";
import {Metadata} from "next";
import React from 'react';
import {Flex} from "@radix-ui/themes"
import {Status} from ".prisma/client";

interface Props {
    searchParams: IssueQuery
}
export const metadata:Metadata={
    title:"Issue Tracker - Dashboard",
    description:"View a summary of project issues"
}


const IssuesPage = async ({searchParams}: Props) => {
    console.log(searchParams)
    const statuses = Object.values(Status);
    const {status} = searchParams;
    const pageSize = 10;
    const page = parseInt(searchParams.page) || 1;
    const where = {status: statuses.includes(status) ? status : undefined}
    const issues = await prisma.issue.findMany({
        where,
        orderBy: columnNames
            .includes(searchParams.orderBy)
            ? {[searchParams.orderBy]: "asc"}
            : undefined,
        skip: (page - 1) * pageSize,
        take: pageSize,
    });
    const issueCount = await prisma.issue.count({where});
    return (
        <Flex direction={"column"} gap={"3"}>
            <IssueAction/>
            <IssueTable issues={issues} searchParams={searchParams}/>
            <Pagination searchParams={searchParams} currentPage={page} pageSize={pageSize} itemCount={issueCount}/>
        </Flex>
    );
};

export default IssuesPage;